/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.client.value.impl;

import java.util.ArrayList;
import java.util.List;

import com.genia.toolbox.web.gwt.form.client.value.FormComplexValue;
import com.genia.toolbox.web.gwt.form.client.value.FormValues;

/**
 * implementation of {@link FormComplexValue}.
 */
public class FormComplexValueImpl
    implements FormComplexValue
{
  /**
   * the name of the item.
   */
  private String name;

  /**
   * the values of the item.
   */
  private List<FormValues>values = null;



  /**
   * constructor.
   */
  public FormComplexValueImpl()
  {
    // do nothing.
  }



  /**
   * getter for the name property.
   * 
   * @return the name
   */
  public String getName()
  {
    return name;
  }



  /**
   * setter for the name property.
   * 
   * @param name
   *          the name to set
   */
  public void setName(String name)
  {
    this.name = name;
  }



  /**
   * getter for the values property.
   * 
   * @return the values
   */
  public List<FormValues> getValues()
  {
    return values;
  }



  /**
   * setter for the values property.
   * 
   * @param values
   *          the values to set
   */
  public void setValues(List<FormValues> values)
  {
    if (values != null && values.isEmpty()) {
      this.values = null;
    }
    else {
      this.values = values;
    }
  }



  /**
   * returns the value of this item. If this item has more than one value, this
   * method returns the first value.
   * 
   * @return the value of this item
   */
  public FormValues getValue()
  {
    final List<FormValues> values = getValues();
    if (values == null || values.isEmpty()) {
      return null;
    }
    return values.get(0);
  }



  /**
   * set the value of this item.
   * 
   * @param value
   *          the value to set
   */
  public void setValue(final FormValues value)
  {
    if (value == null) {
      values = null;
    }
    else {
      if (values == null) {
        values = new ArrayList<FormValues>();
      }
      else {
        values.clear();
      }
      values.add(value);
    }
  }
}
